//
// Created by maqz on 2018/4/2.
// Copyright (c) 2018 毛毛驴. All rights reserved.
//

#import "LKBoTongFeedbackWebViewController.h"
#import "LKBoTongFeedbackWebViewController+Navigation.h"


@implementation LKBoTongFeedbackWebViewController

NSString *scriptCallbackMethod = @"callbackMethod";
NSString *scriptMethodName = @"methodName";
NSString *scriptParams = @"params";

NSString *BotongApplicationCenterShareJSHandler = @"BotongApplicationCenterShareJSHandler";
NSString *BotongApplicationCenterNormalJSHandler = @"BotongApplicationCenterNormalJSHandler";

NSString *PassportId = @"passportId";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.progressView.tintColor = [self hexColor:(0x4390E1)];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _statusBarStyle = [UIApplication sharedApplication].statusBarStyle;
    _navigationBarTitleTextAttributes = self.navigationController.navigationBar.titleTextAttributes;
    _navigationBarTranslucent = self.navigationController.navigationBar.translucent;
    if (@available(iOS 11, *)) {
        _navigationBarShadowImage = self.navigationController.navigationBar.shadowImage;
    } else {
        _navigationBarLine = [self findShadowImageViewUnder:self.navigationController.navigationBar];
        _navigationBarLineIsHidden = _navigationBarLine.hidden;
    }
    

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    // 刷新导航栏的方法放到 viewWillAppear 里会有问题
     [self setupNacigationBarWithType:_naviType];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = _statusBarStyle;
    self.navigationController.navigationBar.barTintColor = nil;
    [self.navigationController.navigationBar setTitleTextAttributes:_navigationBarTitleTextAttributes];
    self.navigationController.navigationBar.translucent = _navigationBarTranslucent;
    if (@available(iOS 11, *)) {
        [self.navigationController.navigationBar setShadowImage:_navigationBarShadowImage];
    } else {
        _navigationBarLine.hidden = _navigationBarLineIsHidden;
    }
    
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    
}

- (UIImageView *)findShadowImageViewUnder:(UIView *)view {
    
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    
    for (UIView *subView in view.subviews) {
        UIImageView *imageView = [self findShadowImageViewUnder:subView];
        if (imageView) {
            return  imageView;
        }
    }
    return nil;
}

- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData
                                                         options:NSJSONReadingMutableContainers
                                                           error:&err];
    if(err)
    {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dict;
}

- (NSArray *)filterContacts:(NSArray *)selectedIds origSelectedArray:(NSArray *)origSelectedArray supplementArray:(NSArray *)supplementArray filterKey:(NSString *)filterKey {
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF[%@] IN %@", filterKey, selectedIds];
    NSMutableArray *tempArr = [NSMutableArray arrayWithArray:[origSelectedArray filteredArrayUsingPredicate:predicate]];
    [tempArr addObjectsFromArray:supplementArray];
    
    return tempArr.copy;
    
//    NSMutableArray *temp = [NSMutableArray new];
//    [origSelectedArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        NSDictionary *dict = (NSDictionary *)obj;
//        if ([selectedIds containsObject:dict[PassportId]]) {
//            [temp addObject:dict];
//        }
//
//    }];
//
//    [temp addObjectsFromArray:supplementArray];
//    return temp.copy;
}

- (UIColor *)hexColor:(NSInteger)hexValue {
    CGFloat red = (CGFloat)((hexValue & 0xFF0000) >> 16) / 255.0;
    CGFloat green = (CGFloat)((hexValue & 0xFF00) >> 8) / 255.0;
    CGFloat blue = (CGFloat)((hexValue & 0xFF)) / 255.0;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}

@end
